const mongoose = require('mongoose');

// 定义Category Schema
const categorySchema = new mongoose.Schema({
  _id: {
    type: Number,
    required: true
  },
  name: {
    type: String,
    required: [true, '分类名称是必需的'],
    trim: true
  },
  description: {
    type: String,
    default: ''
  }
});

// 更新分类的方法
categorySchema.methods.updateCategory = function (name, description) {
  this.name = name;
  this.description = description;
  return this.save();
};

// 静态验证方法
categorySchema.statics.validateCategory = function (categoryData) {
  const errors = [];
  
  if (!categoryData.name || categoryData.name.trim() === '') {
    errors.push('分类名称是必需的');
  }
  
  return {
    valid: errors.length === 0,
    errors
  };
};

// 创建Category模型
const Category = mongoose.model('Category', categorySchema);

// 初始分类数据
const categories = [
  { id: 1, name: '技术', description: '技术相关文章' },
  { id: 2, name: '生活', description: '生活相关文章' },
  { id: 3, name: '教程', description: '教程类文章' }
];

module.exports = {
  Category,
  categories
};